package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2024-05-25 15:22
 */
@Slf4j
@Component("1003")
public class VIPStrategyImpl implements PaidRecordStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Lazy //解决循环依赖问题
    @Autowired
    private UserInfoService userInfoService;


    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 处理购买项目类型VIP
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]处理购买项目类型VIP");
        //3.处理购买项目类型为VIP会员
        //3.1 根据入参订单编号查询购买是否存在
        LambdaQueryWrapper<UserVipService> vipServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        vipServiceLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(vipServiceLambdaQueryWrapper);
        if (count == 0) {
            //3.1 保存本次会员购买记录
            UserVipService userVipService = new UserVipService();
            userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
            userVipService.setUserId(userPaidRecordVo.getUserId());
            //3.1.1 根据用户ID查询用户信息 判断当前用户是否为VIP
            UserInfoVo userInfo = userInfoService.getUserInfo(userPaidRecordVo.getUserId());
            Boolean isVIP = false;
            if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
                isVIP = true;
            }
            //3.1.2 如果是普通用户 本次会员起始时间=当前时间 过期时间：当前时间+会员服务月数
            //3.2 查询套餐信息
            VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
            if (!isVIP) {
                //普通用户 新开会员
                userVipService.setStartTime(new Date());
                DateTime expireDate = DateUtil.offsetMonth(new Date(), vipServiceConfig.getServiceMonth());
                userVipService.setExpireTime(expireDate);
            } else {
                //3.1.3 如果是VIP-续期会员 本次会员起始时间=当前会员过期时间 过期时间：当前会员过期时间+会员服务月数
                DateTime startTime = DateUtil.offsetDay(userInfo.getVipExpireTime(), 1);
                userVipService.setStartTime(startTime);
                userVipService.setExpireTime(DateUtil.offsetMonth(startTime, vipServiceConfig.getServiceMonth()));
            }
            userVipServiceMapper.insert(userVipService);
            //3.2 用户表会员过期时间
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(userVipService.getExpireTime());
            userInfoMapper.updateById(BeanUtil.copyProperties(userInfo, UserInfo.class));
        }
    }
}
